﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_PayLog")]
    public class T_PayLog : IEntity<T_PayLog>
    {
        #region Fileds
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string PayNo { get; set; }

        public DateTime PayDate { get; set; }

        public int TradeId { get; set; }

        public decimal Price { get; set; }

        /// <summary>
        /// 支付结果描述
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string PayMessage { get; set; }
        /// <summary>
        /// 支付返回原文
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(1024)]
        public string PayBackString { get; set; }
        /// <summary>
        /// 支付返回时间
        /// </summary>
        public DateTime PayBackDate { get; set; }

        /// <summary>
        /// 答复内容
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(1024)]
        public string AnswerString { get; set; }
        /// <summary>
        /// 答复时间
        /// </summary>
        public DateTime AnswerDate { get; set; }

        /// <summary>
        /// 支付状态
        /// 1：新支付
        /// 2：支付成功
        /// </summary>
        public int PayStatus { get; set; }
        #endregion

        public override T_PayLog Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_PayLog Find(System.Linq.Expressions.Expression<Func<T_PayLog, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.PayLog.FirstOrDefault(where);
                if (item != null)
                {
                    this.AnswerDate = item.AnswerDate;
                    this.AnswerString = item.AnswerString;
                    this.Id = item.Id;
                    this.PayBackDate = item.PayBackDate;
                    this.PayBackString = item.PayBackString;
                    this.PayDate = item.PayDate;
                    this.PayMessage = item.PayMessage;
                    this.PayNo = item.PayNo;
                    this.PayStatus = item.PayStatus;
                    this.Price = item.Price;
                    this.TradeId = item.TradeId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_PayLog, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.PayLog.Any(where);
            }
        }
    }
}
